import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class Lesson10 {

    private static AtomicInteger atomicInteger = new AtomicInteger(1);

    private static ExecutorService executorService = new ThreadPoolExecutor(10, 10, 1, TimeUnit.HOURS, new ArrayBlockingQueue(10), r -> {
        Thread thread = new Thread(r);
        thread.setDaemon(true);
        thread.setName("custom-executor-" + atomicInteger.incrementAndGet());
        return thread;
    });

    /**
     *  在两个完成的阶段其中之一上调用消费函数
     *  和前一个例子很类似了，只不过我们调用的是消费者函数 (Function变成Consumer):
     *
     * */
    public static void main(String[] args) {

        String message = "Message";

        CompletableFuture completableFuture =  CompletableFuture
                .completedFuture(message)
                .thenApplyAsync(s -> {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return s.toUpperCase();
                }, executorService)
                .acceptEither(CompletableFuture.completedFuture(message).thenApplyAsync(s -> s.toLowerCase()), System.out::println);
        //completableFuture.join();
    }
}
